#!/bin/bash
set -o errexit # Set the shell option to exit on error

DIRNAME=`dirname $0`
source ${DIRNAME}/action_functions.bash
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'

function createHistory(){
  cat > /etc/profile.d/history.sh << \EOF
export HISTTIMEFORMAT="[%F %T] "
export PROMPT_COMMAND='
command_history="$( history 1 |sed "s/^ *\([0-9]*\) *\[[0-9]*-[0-9]*-[0-9]* *[0-9]*:[0-9]*:[0-9]*] *\(.*\)$/\2   #seq=[\1]/g")"
command_history_vv=${command_history}" $( who am i | sed -r "s|^([^ \t]+)[ \t]+([^ \t]+)[ \t]+(.+)[ \t]+(\(.+\))|user=[\1] tty=[\2] date=[\3] ip=\4|") pwd=[`pwd`]"
echo "$command_history_vv" >> /var/log/usercmd.log
'
EOF
  touch /var/log/usercmd.log
  chmod 622 /var/log/usercmd.log
}


function convention(){
  # 默认shell
  dpkg-reconfigure bash > /dev/null 2>&1
  # 默认编辑器
  update-alternatives --set editor /usr/bin/vim.basic > /dev/null 2>&1

  #给用户赋权无密码sudo，添加到 /etc/sudoers.d/目录
  USER=$(getent passwd 1000 | cut -d: -f1)
  if [ -z "$USER" ]; then
    echo "No user with uid 1000 found."
    exit 1
  fi
  echo "$USER ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/$USER

  # 创建开机自启动配置脚本模拟init的rc.local
  cat > /etc/rc.local << \EOF
#!/bin/bash

EOF
chmod +x /etc/rc.local

  cat > /lib/systemd/system/rc-local.service << \EOF
[Service]
ExecStart=/etc/rc.local start
[Install]  
WantedBy=multi-user.target  
Alias=rc-local.service
EOF

  #ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
  systemctl daemon-reload
  systemctl enable --now rc-local > /dev/null 2>&1

  # 关闭swap
  swapoff -a
  sed -i 's/.*swap.*/#&/' /etc/fstab

  sed -i '/^pool /s@pool@#pool@g' /etc/chrony/chrony.conf
  sed -i '/^#pool ntp/a\pool ntp3.aliyun.com iburst' /etc/chrony/chrony.conf
  sed -i '/^#pool ntp/a\pool ntp2.aliyun.com iburst' /etc/chrony/chrony.conf
  sed -i '/^#pool ntp/a\pool ntp1.aliyun.com iburst' /etc/chrony/chrony.conf
  sed -i '/^#pool ntp/a\pool ntp0.aliyun.com iburst' /etc/chrony/chrony.conf
  systemctl enable --now chronyd > /dev/null 2>&1

  if [ ! -f /etc/issue.bak ]; then
    cat /etc/issue > /etc/issue.bak
    cat /dev/null > /etc/issue
  fi

  # 关闭swap
  swapoff -a
  sed -i 's/.*swap.*/#&/' /etc/fstab

  rm -f  /usr/lib/systemd/system/ctrl-alt-del.target
  echo "set ts=4" >> /root/.vimrc
  echo "set expandtab" >> /root/.vimrc
  echo "set shiftwidth=4" >> /root/.vimrc
  echo "set autoindent" >> /root/.vimrc
  echo "set paste" >> /root/.vimrc
  
}

function localization(){
  timedatectl set-timezone Asia/Shanghai
  cat > /etc/profile.d/tz.sh << \EOF
export TZ=Asia/Shanghai
EOF
}

function deal(){
  createHistory
  convention
  localization
	
}

check(){
# 搜索/etc/sudoers.d目录，如果有文件则遍历所有文件
for file in /etc/sudoers.d/*; do
    # 判断文件是否存在
    if [ $(basename "$file") == "README" ]; then continue; fi
    if [ -f "$file" ]; then
        # 获取文件名，即用户名
        USER=$(basename "$file")
        # 通过英文输出警告
        echo -e "${yellow}Warning:${plain} The user $USER has been granted sudo access without password."
    fi
done
}

function main(){
  action $0 deal
  check
}

main $*
